//
//  ReportCell.swift
//  ANOA
//
//  Created by zwb on 2016/11/14.
//  Copyright © 2025 aowang. All rights reserved.
//

import UIKit
// 多选
class ReportCell: UITableViewCell {

    lazy var detailLabel: UILabel = {
        let v = UILabel()
        v.numberOfLines = 2
        v.textColor = UIColor(kshex: "FF7E00")
        v.textAlignment = .right
        return v
    }()
    @IBOutlet weak var titleLabel: UILabel!

    fileprivate var itemNames = [String]()
    fileprivate var itemIDs = [String]()
    fileprivate var defalutIDs = [String]()
    fileprivate var defalutNames = [String]()

    var selectAction: ((String) -> Void)?

    var reportParameter: ReportDetail? {
        didSet {

            defalutIDs = [String]()
            defalutNames = [String]()

            var titleShow = reportParameter?.titleCs
            if containsChineseCharacter(reportParameter!.titleNm.unwrap) {
                titleShow = reportParameter?.titleNm
            }
            titleLabel.text = titleShow
            if reportParameter?.ifSession == "1" {
                

                itemNames = USERINFO.managredUnitNm.unwrap.components(separatedBy: ",")
                itemIDs = USERINFO.managredUnit.unwrap.components(separatedBy: ",")

                if reportParameter?.defaultVal == "managred_unit" ||
                    reportParameter!.defaultVal == "managred_unit_nm" ||
                    reportParameter?.defaultVal == "managred_sp_unit" ||
                    reportParameter?.defaultVal == "managred_my_unit" ||
                    reportParameter?.defaultVal == "managred_sl_unit" ||
                    reportParameter?.defaultVal == "managred_yz_unit" { // 所有管理的公司  多个
                    defalutIDs = itemIDs
                    defalutNames = itemNames
                    detailLabel.text = itemNames.joined(separator: ",")
                    addChar(itemIDs.joined(separator: ","))
                } else if reportParameter?.defaultVal == "org_code" || reportParameter?.defaultVal == "org_name" {
                    defalutIDs = [USERINFO.cUnitnameIdHs!]
                    defalutNames = [USERINFO.cUnitnameHs!]
                    detailLabel.text = USERINFO.cUnitnameHs!
                    addChar(USERINFO.cUnitnameIdHs!)
                } else {
                    detailLabel.text = "[未识别配置]"

                }

            } else {
                if let remark = reportParameter?.sRemarks!.components(separatedBy: ";") {
                    guard remark.count > 0 else {
                        return
                    }

                    // 截取选项 "s_amount_z#?销量;s_money#?销售金额;s_agio#?折扣;s_month#?月份;s_month"
                    // ["销量", "销售金额", "折扣", "月份"]"
                    itemNames = remark.compactMap { (a) -> String? in
                        if a.contains("?") {
                            return String(a[a.index(a.firstIndex(of: "?")!, offsetBy: 1)...])
                        } else {
                            return nil
                        }
                    }

                    // ["s_amount_z", "s_money", "s_agio", "s_month"]"
                    itemIDs = remark.compactMap { (a) -> String? in
                        if a.contains("?") {
                            return String(a[..<a.index(a.firstIndex(of: "?")!, offsetBy: -1)])
                        } else {
                            return nil
                        }
                    }

                 
                 
                        let parameter = remark.last?.components(separatedBy: ",") ?? [remark.last!]
                        var tempNames = ""
                
                        defalutIDs = parameter

                        for item in parameter {
                            guard let index = itemIDs.firstIndex(of: item) else {
                                continue
                            }

                            tempNames += itemNames[index]
                            tempNames += ","

                            defalutNames.append(itemNames[index])
                        }
                   
                        detailLabel.text = tempNames
                        self.addChar(remark.last!)
 
                }
            }
        }
    }

    fileprivate func addChar(_ para: String) {
        if let selectAction = self.selectAction {
            let postChar = self.reportParameter?.sPostChar ?? ""
            let sPrefixChar = self.reportParameter?.sPrefixChar ?? ""
            selectAction(sPrefixChar + para + postChar)
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code

        let tap = UITapGestureRecognizer(target: self, action: #selector(touchIt))
        tap.numberOfTapsRequired = 1
        addGestureRecognizer(tap)
        self.contentView.addSubview(detailLabel)
        detailLabel.snp.makeConstraints { make in
            make.left.equalTo(titleLabel.snp.right)
            make.right.equalToSuperview().offset(-35)
            make.top.bottom.equalToSuperview()
        }
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    @objc fileprivate func touchIt() {
        
        
        if reportParameter?.ifSession == "1" {
            var ttt:OrgSelectType!
            if reportParameter?.sDx == "1" {
                ttt = .multi
            } else {
                ttt = .single
            }
            
            KSOrgSelectUtil.initWithNav(type: ttt, sessionK: reportParameter?.sessionKey ?? "",selectedIDs: defalutIDs,backClosure: { nodes in


               let namestr = nodes.compactMap { $0.name }.joined(separator: ",")
               let idstr = nodes.compactMap { $0.id }.joined(separator: ",")

               self.defalutIDs = nodes.compactMap { $0.id }
               self.defalutNames = nodes.compactMap { $0.name }
               self.detailLabel.text = namestr
               self.addChar(idstr)


            }){ vc in
               let topViewController = RouterUtils.currentTopViewController()
               topViewController!.present_FullScreen(to: vc)
            }
        } else {
            let selectView = SelectView()
            selectView.itemNames = itemNames
            selectView.itemIDs = itemIDs
            selectView.selectedNames = defalutNames
            selectView.selectedIndexs = defalutIDs
            selectView.selectedClosure = { [unowned self] (selectedIndexs, selectedValues) in
    
                self.detailLabel.text = selectedValues
                self.addChar(selectedIndexs)
            }
    
            selectView.show()
        }
               
 
 
    }

}
